package com.qfedu.controller;


import com.qfedu.entity.Depart;
import com.qfedu.entity.DepartNumDate;
import com.qfedu.entity.ResponseData;
import com.qfedu.service.DepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping
public class DepartController {

    @Autowired
    private DepartService departService;

    @RequestMapping("/addDepart")
    @ResponseBody
    public ResponseData addDepart(Depart depart){
        System.out.println(depart);

        boolean result = departService.addDepart(depart);

        ResponseData responseData = new ResponseData();

        if(result){
            responseData.setCode(ResponseData.SUCCESS_CODE);// 成功
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else{
            responseData.setCode(ResponseData.FAIL_CODE);// 成功
            responseData.setMsg(ResponseData.FAIL_MSG);
        }

        return responseData;

    }


    @RequestMapping("/findAllDepart")
    @ResponseBody
    public ResponseData findAllDepart(int page,int limit){

        return  departService.findAllDepart(page, limit);

    }

    @RequestMapping("/deleteDepartById")
    @ResponseBody

    public ResponseData deleteDepartById(int id){

        boolean result = departService.deleteDepartById(id);

        ResponseData responseData = new ResponseData();

        if (result){
            responseData.setCode(ResponseData.SUCCESS_CODE);// 成功
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else{
            responseData.setCode(ResponseData.FAIL_CODE);// 成功
            responseData.setMsg(ResponseData.FAIL_MSG);
        }
        return responseData;

    }


    @RequestMapping("/updateDepart")
    @ResponseBody
    public ResponseData updateDepart(Depart depart){
        boolean result = departService.updateDepart(depart);
        ResponseData responseData = new ResponseData();
        if (result){
            responseData.setCode(ResponseData.SUCCESS_CODE);// 成功
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else {
            responseData.setCode(ResponseData.FAIL_CODE);// 成功
            responseData.setMsg(ResponseData.FAIL_MSG);
        }
        return responseData;
    }

    @RequestMapping("/getDepartNumData")
    public ResponseData<DepartNumDate> getDepartNumData(){

        DepartNumDate departNumDate = departService.getDepartNumData();
        /**
         * 按照格式返回 json 数据
         */
        ResponseData<DepartNumDate> responseData = new ResponseData<>();

        responseData.setCode(ResponseData.SUCCESS_CODE);
        responseData.setMsg(ResponseData.SUCCESS_MSG);
        responseData.setData(departNumDate);

        return responseData;
    }

}
